package com.example.sharesinfo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sharesinfo.R;
import com.example.sharesinfo.adapter.CommonAdapter;
import com.example.sharesinfo.adapter.CommonViewHolder;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.bean.QABean;
import com.example.sharesinfo.manager.DialogManager;
import com.example.sharesinfo.model.PositionModel;
import com.example.sharesinfo.model.QAModel;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.view.DialogView;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


public class QAActivity extends BaseUIActivity implements View.OnClickListener {
    private static final String LOGTAG = "QAActivity:";
    private static final int QUERYDATASUCCESS = 1001;
    private static final int QUERYDATAEMPTY = 1002;
    private static final int QUERYDETAILSSUCCESS = 1003;
    private static final int QUERYDETAILSFAILED = 1004;
    //问答
    private LinearLayout ll_wenda;
    private DialogView mWenDaView;

    private ImageButton ib_back;
    private TextView tv_my_qa;
    private TextView tv_put_question;
    //问答列表
    private RecyclerView mQAView;
    private CommonAdapter<QAModel> mQAAdapter;
    private List<QAModel> mList = new ArrayList<>();

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case QUERYDATASUCCESS:
                    if (mList.size() > 0) {
                        mList.clear();
                    }
                    QABean result = (QABean) msg.obj;
                    mList.addAll(result.getData().getData());
                    mQAAdapter.notifyDataSetChanged();
                    break;
                case QUERYDATAEMPTY:
                    break;
                case QUERYDETAILSSUCCESS:
                    QADetailActivity.startActivity(QAActivity.this, msg.obj.toString());
                    break;
                case QUERYDETAILSFAILED:
                    break;
                case 404:
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qa);
        initView();
        createWenDaDialog();
        queryData();
    }

    private void initView() {
        ib_back = findViewById(R.id.ib_back);
        tv_my_qa = findViewById(R.id.tv_my_qa);
        tv_put_question = findViewById(R.id.tv_put_question);
        ib_back.setOnClickListener(this);
        tv_my_qa.setOnClickListener(this);
        tv_put_question.setOnClickListener(this);
        ll_wenda = findViewById(R.id.ll_wenda);
        ll_wenda.setOnClickListener(this);
        mQAView = findViewById(R.id.mQAView);
        mQAView.setLayoutManager(new LinearLayoutManager(this));
        mQAView.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL));
        mQAAdapter = new CommonAdapter<>(mList, new CommonAdapter.OnBindDataListener<QAModel>() {
            @Override
            public void onBindViewHolder(final QAModel model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.tv_content, model.getQuestionContent());
                viewHolder.setText(R.id.tv_question_time, model.getQuestionTime());
                if (model.getAnswer_Username().equals("")) {
                    //无人回答
                    viewHolder.getView(R.id.ci_photo).setVisibility(View.GONE);
                    viewHolder.getView(R.id.tv_answer_name).setVisibility(View.GONE);
                    viewHolder.setText(R.id.tv_if_answered, "暂时无人回答....");
                } else {
                    //已经回答
                    viewHolder.setImageUrl(QAActivity.this, R.id.ci_photo, model.getAnswer_ImageUrl());
                    viewHolder.setText(R.id.tv_answer_name, model.getAnswer_Username());
                }
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        queryQaDetails(model.getId());
                    }
                });

            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.qa_list_item;
            }
        });
        mQAView.setAdapter(mQAAdapter);

    }

    /**
     * 获取问答列表详情
     */
    private void queryQaDetails(int id) {
        String url = "http://119.23.221.142/api/user/GetQADetail?QuestionId=" + id;
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                message.what = QUERYDETAILSSUCCESS;
                message.obj = s;
                mHandler.sendMessage(message);
            }

            @Override
            public void OnFailed(String s) {
                message.what = 404;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });

    }

    /**
     * 获取问答列表数据集
     */
    private void queryData() {
        String url = "http://119.23.221.142/api/user/GetQAList";
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                JSONObject object = new JSONObject(s);
                if (object.getString("code").equals("200")) {
                    //查询成功
                    LogUtils.i(LOGTAG + s);
                    QABean bean = new Gson().fromJson(s, QABean.class);
                    message.what = QUERYDATASUCCESS;
                    message.obj = bean;
                    mHandler.sendMessage(message);
                } else {
                    //查询成功
                    message.what = QUERYDATAEMPTY;
                    mHandler.sendMessage(message);
                }

            }

            @Override
            public void OnFailed(String s) {
                //请求失败
                message.what = QUERYDATASUCCESS;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });
    }

    /**
     * @type
     * @explain 创建提问提示框
     * @author
     * @creat time
     */

    private void createWenDaDialog() {
        mWenDaView = DialogManager.getInstance().initView(this,
                R.layout.dialog_send_question);
        //外部点击无效
        mWenDaView.setCancelable(false);
        ImageView iv_back = mWenDaView.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getInstance().hide(mWenDaView);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.tv_my_qa:
                LogUtils.i(LOGTAG + "点击了问答");
                break;
            case R.id.tv_put_question:
                LogUtils.i(LOGTAG + "点击了问答");
                break;
            case R.id.ll_wenda:
                LogUtils.i(LOGTAG + "点击了问答");
                DialogManager.getInstance().show(mWenDaView);
                break;
        }
    }
}
